using MongoDB.Driver;
using System;
using System.Configuration;

namespace SmartLink.MongoHelper
{
    /// <summary>
    /// 表示基于MongoDB实现的仓储上下文的配置信息。
    /// </summary>
    /// <remarks>
    /// 如果您的MongoDB配置不是默认的，您可以在此处修改MongoDB的配置信息，比如
    /// 可以在ServerSettings里指定MongoDB的端口号等。
    /// </remarks>
    public class MongoDBRepositoryContextSettings : IMongoDBRepositoryContextSettings
    {
        private readonly string serverStr = "127.0.0.1";
        private readonly int port = 27017;
        private readonly string dbName = "TPSVehicle";

        public MongoDBRepositoryContextSettings()
        {
            if (ConfigurationManager.AppSettings["MongoDBServer"] != null)
            {
                serverStr = ConfigurationManager.AppSettings["MongoDBServer"];
            }
            if (ConfigurationManager.AppSettings["MongoDBServerPort"] != null)
            {
                port = Int32.Parse(ConfigurationManager.AppSettings["MongoDBServerPort"]);
            }
            if (ConfigurationManager.AppSettings["MongoDBDbName"] != null)
            {
                dbName = ConfigurationManager.AppSettings["MongoDBDbName"];
            }
        }

        #region IMongoDBRepositoryContextSettings Members
        /// <summary>
        /// 获取数据库名称。
        /// </summary>
        public string DatabaseName
        {
            get { return dbName; }
        }
        /// <summary>
        /// 获取MongoDB的服务器配置信息。
        /// </summary>
        public MongoClientSettings ClientSettings
        {
            get
            {
                var settings = new MongoClientSettings();
                settings.Server = new MongoServerAddress(serverStr, port);
                settings.WriteConcern = WriteConcern.Acknowledged;
                return settings;
            }
        }
        /// <summary>
        /// 获取数据库配置信息。
        /// </summary>
        /// <param name="server">需要配置的数据库实例。</param>
        /// <returns>数据库配置信息。</returns>
        public MongoDatabaseSettings GetDatabaseSettings(MongoClient client)
        {
            // 您无需做过多的更改：此处仅返回新建的MongoDatabaseSettings实例即可。
            return new MongoDatabaseSettings();
        }

        #endregion
    }
}
